<%
var content_board = '<div class="form-group clearfix"><select name="board_id" class="js-change-list col-xs-12 form-control input-sm cur">';
var content_list = '<div class="form-group clearfix"><select name="list_id" class="js-change-position col-xs-12 form-control input-sm cur" required>';
var content_position = '';
if (sort_by !== null && sort_direction !== null) {
	card.list.cards.sortByColumn(sort_by, sort_direction);
} else {
	card.list.cards.sortByColumn('position');
}
var current_position = card.list.cards.indexOf(card.list.cards.get(card.attributes.id)) + 1;
if(current_position == 0){
	current_position = 1;
}
var wip_enabled = false;
if (!_.isUndefined(APPS) && APPS !== null ) {
	if (!_.isUndefined(APPS.enabled_apps) && APPS.enabled_apps !== null) {
		if ($.inArray('r_agile_wip',APPS.enabled_apps) !== -1) {
			wip_enabled = true;
		}
	}
}
boards.each(function(board) {
	if (card.attributes.board_id == board.id) {
		content_board += '<option value="' + board.id + '" selected="selected">' + _.escape(board.attributes.name) +  ' '+i18next.t('(current)')+'</option>';
		var filtered_lists = board.lists.where({
			is_archived: 0,
			board_id: card.attributes.board_id
		});
		_.each(filtered_lists, function(list) {
			
			if (card.attributes.list_id == list.id) {
				content_list += '<option value="' + list.id + '" selected="selected">' + _.escape(list.attributes.name) +  ' '+ i18next.t('(current)')+'</option>';
				if ((!_.isUndefined(App.current_board.attributes.sort_by) && App.current_board.attributes.sort_by !== null && App.current_board.attributes.sort_by === 'position') || (_.isUndefined(App.current_board.attributes.sort_by) ||App.current_board.attributes.sort_by === null)) {
					content_position += '<div class="form-group clearfix">';
				} else {
					content_position += '<div class="form-group clearfix hide">';
				}
				for(var i = 1; i <= list.attributes.card_count; i++){
					if (i === 1) {
						content_position += '<select name="position" class="js-position col-xs-12 form-control input-sm cur">';
					}
					if (card.attributes.list_id == list.attributes.id && i == current_position) {
						content_position += '<option value="' + i + '" selected="selected">' + i +  ' '+i18next.t('(current)')+'</option>';
					} else {
							content_position += '<option value="' + i + '">' + i+ '</option>';
					}
				}
				if (card.attributes.list_id != list.attributes.id) {
					var next_position = parseInt(list.attributes.card_count ) + 1;
					content_position += '<option value="' + next_position + '">' + next_position+ '</option>';
				}
				content_position += '</select></div>';
			} else {
				if (wip_enabled && !_.isUndefined(list.attributes.custom_fields) && !_.isEmpty(list.attributes.custom_fields)) {
					var list_custom_fields = JSON.parse(list.attributes.custom_fields);
					var list_card_count = isNaN(list.attributes.card_count) ? 0 : list.attributes.card_count;
					if (!_.isUndefined(list_custom_fields.wip_limit) && !_.isEmpty(list_custom_fields.wip_limit)) {
						if (parseInt(list_card_count) === parseInt(list_custom_fields.wip_limit) && !_.isUndefined(list_custom_fields.hard_wip_limit) && !_.isEmpty(list_custom_fields.hard_wip_limit)) {
							content_list += '<option value="' + list.id + '" disabled>' + _.escape(list.attributes.name) + ' (' + i18next.t('Agile WIP exceeded') + ')</option>';
						} else if(parseInt(list_card_count) > parseInt(list_custom_fields.wip_limit)){
							content_list += '<option value="' + list.id + '">' + _.escape(list.attributes.name) + ' ( > ' + i18next.t('WIP limit') + ')</option>';
						} else {
							content_list += '<option value="' + list.id + '">' + _.escape(list.attributes.name) + '</option>';
						}
					} else {
						content_list += '<option value="' + list.id + '">' + _.escape(list.attributes.name) + '</option>';
					}
				} else {
					content_list += '<option value="' + list.id + '">' + _.escape(list.attributes.name) + '</option>';
				}
			}

		});
	} else {
		if(parseInt(board.attributes.is_closed) === 0 && board.lists.length > 0){
			if(!_.isEmpty(board.attributes.organization_name) && parseInt(board.attributes.organization_id) != 0) {
				content_board += '<option value="' + board.id + '">' + _.escape(board.attributes.name) + ' (' + _.escape(board.attributes.organization_name) + ') </option>';
			} else {
				content_board += '<option value="' + board.id + '">' + _.escape(board.attributes.name) + '</option>';
			}
		}
	}

});
content_board += '</select></div>';
content_list += '</select></div>';
var content = content_board + content_list + content_position;
%>
<%= content %>